package com.lhj.library.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.lhj.library.common.entity.PageResult;
import com.lhj.library.common.enums.IsBorrowFlagEnum;
import com.lhj.library.common.enums.IsDeletedFlagEnum;
import com.lhj.library.common.enums.IsReturnFlagEnum;
import com.lhj.library.domain.convert.BookBoConverter;
import com.lhj.library.domain.convert.RecordBoConverter;
import com.lhj.library.domain.entity.BookBO;
import com.lhj.library.domain.entity.RecordBO;
import com.lhj.library.domain.service.BookDomainService;
import com.lhj.library.infra.basic.entity.Book;
import com.lhj.library.infra.basic.entity.Record;
import com.lhj.library.infra.basic.entity.User;
import com.lhj.library.infra.basic.service.BookService;
import com.lhj.library.infra.basic.service.RecordService;
import com.lhj.library.infra.basic.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

@Service
@Slf4j
public class BookDomainServiceImpl implements BookDomainService {

    @Resource
    private BookService bookService;

    @Resource
    private RecordService recordService;

    @Resource
    private UserService userService;

    @Override
    public Boolean add(BookBO bookBO) {
        if (log.isInfoEnabled()) {
            log.info("BookController.add.bo:{}", JSON.toJSONString(bookBO));
        }
        Book book = BookBoConverter.INSTANCE.convertBoToBook(bookBO);
        book.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        book.setStatus(IsBorrowFlagEnum.UN_BORROW.getCode());
        Integer result = bookService.insert(book);
        return result > 0;
    }

    @Override
    public Boolean update(BookBO bookBO) {
        if (log.isInfoEnabled()) {
            log.info("BookController.update.bo:{}", JSON.toJSONString(bookBO));
        }
        Book book = BookBoConverter.INSTANCE.convertBoToBook(bookBO);
        Integer result = bookService.update(book);
        return result > 0;
    }

    @Override
    public Boolean delete(BookBO bookBO) {
        if (log.isInfoEnabled()) {
            log.info("BookController.delete.bo:{}", JSON.toJSONString(bookBO));
        }
        Book book = BookBoConverter.INSTANCE.convertBoToBook(bookBO);
        book.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        Integer result = bookService.update(book);
        return result > 0;
    }

    @Override
    public BookBO select(BookBO bookBO) {
        if (log.isInfoEnabled()) {
            log.info("BookController.delete.bo:{}", JSON.toJSONString(bookBO));
        }
        Book book = BookBoConverter.INSTANCE.convertBoToBook(bookBO);
        Book queryBook = bookService.queryById(book);
        BookBO queryBookBo = BookBoConverter.INSTANCE.convertBookToBo(queryBook);
        return queryBookBo;
    }

    @Override
    public PageResult<BookBO> selectByPage(BookBO bookBO) {
        if (log.isInfoEnabled()) {
            log.info("BookController.selectByPage.bo:{}", JSON.toJSONString(bookBO));
        }
        PageResult<BookBO> pageResult = new PageResult<>();
        pageResult.setPageNo(bookBO.getPageNo());
        pageResult.setPageSize(bookBO.getPageSize());
        int start = (bookBO.getPageNo() - 1) * bookBO.getPageSize();
        Book book = BookBoConverter.INSTANCE.convertBoToBook(bookBO);
        int count = bookService.count(book);
        if (count == 0) {
            return pageResult;
        }
        List<Book> bookList = bookService.selectByPage(book, start, bookBO.getPageSize());
        List<BookBO> bookBOS = BookBoConverter.INSTANCE.convertBookToBoList(bookList);
        pageResult.setResult(bookBOS);
        pageResult.setRecords(bookBOS);
        return pageResult;
    }

    @Override
    public Boolean borrow(RecordBO recordBO) {
        if (log.isInfoEnabled()) {
            log.info("BookController.borrow.bo:{}", JSON.toJSONString(recordBO));
        }
        Record record = RecordBoConverter.INSTANCE.convertBoToRecord(recordBO);
        record.setStatus(IsReturnFlagEnum.BORROW.getCode());
        record.setBorrowTime(String.valueOf(LocalDateTime.now()));
        Book book = new Book();
        book.setId(record.getBookId());
        book.setStatus(IsBorrowFlagEnum.BORROW.getCode());
        bookService.update(book);
        return recordService.insert(record) > 0;
    }

    @Override
    public Boolean returned(RecordBO recordBO) {
        if (log.isInfoEnabled()) {
            log.info("BookController.returned.bo:{}", JSON.toJSONString(recordBO));
        }
        Record record = RecordBoConverter.INSTANCE.convertBoToRecord(recordBO);
        Record queryRecord = recordService.queryByCondition(record);

        queryRecord.setReturnTime(String.valueOf(LocalDateTime.now()));
        queryRecord.setStatus(IsReturnFlagEnum.UN_BORROW.getCode());
        Book book = new Book();
        book.setId(queryRecord.getBookId());
        book.setStatus(IsBorrowFlagEnum.UN_BORROW.getCode());
        bookService.update(book);
        return recordService.update(queryRecord) > 0;
    }
}
